<?php
/**
 * 执行Action逻辑，并输出页面结果
 *
 * @package mvc
 * @author 
 **/
class ActionServer {
	
	/**
	 * 请求的数据
	 * 
	 */
	//private $_request = array();
	

	/**
	 * 返回给用户，用于页面显示的数据
	 * 
	 */
	private $_response = array ();
	
	/**
	 * 请求路径
	 * 可以是index/index_topic这样的二级路径
	 * 也可以是index这样的一级路径
	 * @var string
	 **/
	private $_do_path = '';
	
	/**
	 * action文件路径
	 *
	 * @var string
	 **/
	private $_action_path = '';
	
	/**
	 * view文件路径
	 *
	 * @var string
	 **/
	private $_view_path = '';
	
	/**
	 * 请求的Action类名
	 *
	 * @var string
	 **/
	private $_classname = '';
	
	/**
	 * 初始化
	 *
	 * @return void
	 * @author 
	 **/
	private function _init() {
		$this->_do_path = $_REQUEST ['do'];
		if (empty ( $this->_do_path )) {
			$this->_do_path = 'index/index';
		}
		//action_path 处理
		if (false !== strpos ( $this->_do_path, '/' )) {
			list ( $dir, $file ) = explode ( '/', $this->_do_path );
			// 把tour_abroad此类path转换为TourAbroad
			Utils::toCamel ( $file );
			$this->_action_path = ACTION_DIR . $dir . SLASH . $file . 'Action.cls.php';
			$this->_classname = $file . 'Action';
		} else {
			$this->_action_path = ACTION_DIR . ucfirst ( $this->_do_path ) . 'Action.cls.php';
			$this->_classname = ucfirst ( $this->_do_path ) . 'Action';
		}
		$this->_view_path = TEMPLATE_DIR . $this->_do_path . '.html';
	}
	
	/**
	 * 处理请求
	 *
	 * @return void
	 * @author 
	 **/
	public function process() {
		// 初始化
		$this->_init ();
		if (! file_exists ( $this->_action_path )) {
			exit ( "Action class Error:File Not Found " . $this->_action_path );
		}
		// Action执行

		require_once $this->_action_path;
		$action = new $this->_classname ( );
		//var_dump($this->_request);
		$action->before ();
		$action->main ();
		$action->after ();

		// 获取响应
		$response = $action->getResponse ();
		// view 处理
		$tpl = new SmartTemplate ( $this->_view_path );
		$tpl->assign ( $response );
		
		$tpl->output ();
	
	}
}